﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using EncryptionLib.Encryption;
using EncryptionLib.Users;


namespace TotalCommander_v3
{
    public partial class frmDecryptProcess : Form
    {
        public string outputDir = string.Empty;
        public string sFile = string.Empty;

        public frmDecryptProcess()
        {
            InitializeComponent();
        }

        private void frmDecryptProcess_Load(object sender, EventArgs e)
        {

        }
        void decryptor_DecryptAsyncProgress(object sender, EncryptionProgressArgs args)
        {
            if (args.Progress == -1)
            {
                //MessageBox.Show(this, 
                //    string.Format("An unexpected error has occurred when decrypting.\r\n\r\nError: {0}", args.ProgressExeption.Message),
                //    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                if (args.ProgressExeption.GetType() == typeof(System.Security.Cryptography.CryptographicException))
                    MessageBox.Show("You don't have permission to decrypt this file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                    MessageBox.Show("Error when decrypting", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                Close();
            }
            else
            {
                prbarDecrypt.Value = args.Progress;
                if (args.Progress == 100)
                {
                    this.Close();
                }
                Debug.WriteLine(string.Format("Decrypt: {0}", args.Progress));
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (sFile == string.Empty || outputDir == string.Empty)
            {
                this.timer1.Enabled = false;
                this.Close();
                return;
            }
            try
            {
                UserData receiver = frmMain.m_userManager.LoginByEmail(frmMain.sEmail, frmMain.sPassphrase);
                if (receiver == null)
                {
                    MessageBox.Show(this, "This user is not existed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.timer1.Enabled = false;
                    this.Close();
                    return;
                }

                Decryptor decryptor = new Decryptor();

                decryptor.EncryptedFile = sFile;
                decryptor.OutputPath = outputDir;
                decryptor.Receiver = receiver;

                //decryptor.Decrypt(frmMain.sPassphrase);

                decryptor.DecryptAsyncProgress += new EncryptionProgressHandler(decryptor_DecryptAsyncProgress);
                decryptor.DecryptAsync(this, frmMain.sPassphrase);

                this.timer1.Enabled = false;
                //MessageBox.Show(this, "Successfully!", "Congrats", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                this.timer1.Enabled = false;
                MessageBox.Show(this, string.Format("Error:\r\n{0}", ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
